﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Reflection;

namespace Facebook.Api
{
    public class FacebookList<T> : List<T>, IFacebookList
        where T : FacebookObjectBase, new()
    {
        public FacebookList(XElement content, String itemXPath)
            : base(FacebookList<T>.GetObjects(content, itemXPath)) { }

        private static IEnumerable<T> GetObjects(XElement content, String itemXPath)
        {
            return
                from element in content.XPathSelectElements("//" + itemXPath)
                select FacebookList<T>.GetFacebookObject(element);
        }

        private static T GetFacebookObject(XElement content)
        {
            var obj = new T();
            obj.Init(content);
            return obj;
        }

        public String ItemXPath { get; set; }
    }
}